package com.itheima.test.UDP.unicast;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Service {//接收方
    public static void main(String[] args) throws IOException {
        //接受方的码头,指定端口1000,
        DatagramSocket socket = new DatagramSocket(1000);
        //创建接受数据的包裹
        byte[] bytes = new byte[1024];
        DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
        //接受礼物,用新的箱子去装礼物
        socket.receive(packet);
        //getDate()方法获取包裹中的数据
        byte[] data = packet.getData();
        //输出礼物
        System.out.println(new String(data,0,packet.getLength()));//参数（礼物数组，开始索引，长度）
        System.out.println(new String(bytes,0,packet.getLength()));
        //释放资源
        socket.close();
    }
}
